//
//  KKVistorView.swift
//  KKWeibo
//
//  Created by kassia on 16/7/9.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SnapKit
class KKVistorView: UIView {
    
    
    var loginClosure:(()->())?//登录闭包
    
    //旋转图片
    private lazy var cycleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //设置遮罩
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //添加主页图片
    private lazy var iconImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //添加文字title
    private lazy var messageLable: UILabel = {
        let lable = UILabel()
    
        lable.font = UIFont.systemFontOfSize(13)
        lable.textColor = UIColor.grayColor()
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        //多行显示
        lable.numberOfLines = 0
        lable.textAlignment = .Center
        return lable

    }()
    
    //登录
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()//创建按钮
        
        button.setTitle("注册", forState: .Normal)//文字
        
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)//字体颜色
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)//高亮状态字体颜色
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)//背景
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)//字体大小
        
         button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)//添加点击事件
        
        return button
    }()
    
    //登录
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()//创建按钮
        
        button.setTitle("登录", forState: .Normal)//文字
        
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)//字体颜色

        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)//高亮状态字体颜色
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)//背景
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)//字体大小
        
         button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)//添加点击事件
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setUI()
        
        //添加动画
        startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setUI() {
    
    //设置颜色
    backgroundColor = UIColor(white: 237 / 255, alpha: 1)
    
    //添加到界面
    addSubview(cycleImageView)
    addSubview(coverView)
    addSubview(messageLable)
    addSubview(iconImage)
    addSubview(registerButton)
    addSubview(loginButton)
    
    //自动布局
    cycleImageView.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
    }
    
    coverView.snp_makeConstraints { (make) -> Void in
       make.center.equalTo(cycleImageView)
    }
    
    iconImage.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(cycleImageView)
    }
    messageLable.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(coverView)
        make.width.equalTo(224)
        make.top.equalTo(coverView.snp_bottom)
    }
    
    registerButton.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(messageLable.snp_bottom).offset(10)
        make.leading.equalTo(messageLable)
        make.size.equalTo(CGSizeMake(100, 35))
    }
    
    loginButton.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(registerButton)
        make.trailing.equalTo(messageLable)
        make.size.equalTo(registerButton)
    }
    }
    
    //注册按钮的点击事件
    @objc private func registerButtonAction() {
        
          print(registerButtonAction)
        loginClosure?()
    
    }

    //登录按钮的点击事件
    @objc private func loginButtonAction() {
        
        print(loginButtonAction)
        loginClosure?()

    }
    
    //开启动画
    private func startAnimation() {
        
        //创建动画
        let animaton = CABasicAnimation(keyPath: "transform.rotation")
        
        animaton.toValue = 2 * M_PI //旋转的角度
        
        animaton.duration = 20//持续时间
        
        animaton.repeatCount = MAXFLOAT//重复次数
        
        animaton.removedOnCompletion = false//设置动画不移除

        cycleImageView.layer.addAnimation(animaton, forKey: nil)//添加动画
        
    }
    
    //提供修改信息的访客视图
    func updataCostomerView(title: String?, imageName: String?) {
    
        //根据t和imageName是否为nil来判断是否加动画图
        if let t = title, imageName = imageName {
        
            messageLable.text = t
            
            iconImage.image = UIImage(named: imageName)
            
            cycleImageView.hidden = true
        
        } else {
            
            //由于刚开始设置的值是首页的,所以不需要再设置,只需要把动画开启即可
            cycleImageView.hidden = false

            startAnimation()
        }
    }
}
